package cn.crm.Service.Impl;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;

import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.usermodel.HSSFFont;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.hssf.util.CellRangeAddress;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

import cn.crm.Service.IProductService;
import cn.crm.common.ServerResponse;
import cn.crm.entity.Product;
import cn.crm.mapper.ProductMapper;
@Service
public class ProductServiceImpl implements IProductService{

	@Autowired
	private ProductMapper productmapper;
	
	@Override
	public ServerResponse pageList(Integer page, Integer limit, Product product) {
		// TODO Auto-generated method stub
		PageHelper.startPage(page, limit);
		List<Product> pagelist = productmapper.pageList(product);
		PageInfo pageInfo = new PageInfo<>(pagelist);
		int count = (int) pageInfo.getTotal();
		return ServerResponse.createSuccess("查找成功", count, pagelist);
	}

	@Override
	public ServerResponse insert(Product product) {
		// TODO Auto-generated method stub
		int count = productmapper.insert(product);
		if (count == 1) {
			return ServerResponse.createSuccess("添加成功");
		}else {
			return ServerResponse.createSuccess("添加失败");
		}
	}

	@Override
	public ServerResponse selectIds() {
		// TODO Auto-generated method stub
		List list = productmapper.pageList(new Product());
		return ServerResponse.createSuccess("", list);
	}

	@Override
	public ServerResponse getproductcount() {
		// TODO Auto-generated method stub
		List<Product> list = productmapper.getproductcount();
		return ServerResponse.createSuccess("查找成功", list);
	}

	@Override
	public void exprotExcle(HttpServletResponse response) {
		   // 1.查询用户列表
		   List<Product> list = productmapper.pageList(new Product());
		   for (Product Product : list) {
		     System.out.println(Product);
		   }
		   // 2.写到excel
		   // 1、创建工作簿：Workbook。
		   HSSFWorkbook workbook = new HSSFWorkbook();
		   // 2、创建工作表：Sheet。
		   HSSFSheet sheet = workbook.createSheet("用户列表");
		   // 设置默认的列宽
		   sheet.setDefaultColumnWidth(25);
		   // 2.1创建合并单元格对象
		   CellRangeAddress cellRangeAddress = new CellRangeAddress(2, 6, 1, 12);
		   sheet.addMergedRegion(cellRangeAddress);
		   // 3、创建行：Row。
		   HSSFRow rowTitle = sheet.createRow(2);
		   // 4、创建单元格：Cell。
		   HSSFCell cellTitle = rowTitle.createCell(1);
		   HSSFCellStyle style = createStyle(workbook, 25);
		   cellTitle.setCellStyle(style);
		   cellTitle.setCellValue("用户列表");
		   HSSFRow rowHead = sheet.createRow(7);
		   /* 创建单元格 */
		   String[] array = { "产品名称","图片", "单位", "价格","库存","详情" };
		   for (int i = 0; i < array.length; i++) {
		     HSSFCell cellHead = rowHead.createCell(i + 1);
		     HSSFCellStyle headStyle = createStyle(workbook, 13);
		     cellHead.setCellStyle(headStyle);
		     cellHead.setCellValue(array[i]);
		   }
		   HSSFCellStyle styleCenter = workbook.createCellStyle();
		   styleCenter.setAlignment(HSSFCellStyle.ALIGN_CENTER);// center
		                                      // horizontal
		                                      // alignment
		   styleCenter.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);// 垂直居中
		   // 遍历输出数据库中的数据到excel中
		   for (int i = 0; i < list.size(); i++) {
		     HSSFRow row = sheet.createRow(8 + i);
		     HSSFCell cellId = row.createCell(1);
		     cellId.setCellStyle(styleCenter);
		     cellId.setCellValue(list.get(i).getName());
		     HSSFCell cellImage = row.createCell(2);
		     cellImage.setCellStyle(styleCenter);
		     cellImage.setCellValue(list.get(i).getImage());
		     HSSFCell cellUnit = row.createCell(3);
		     cellUnit.setCellStyle(styleCenter);
		     cellUnit.setCellValue(list.get(i).getUnit());
		     HSSFCell cellPrice = row.createCell(4);
		     cellPrice.setCellStyle(styleCenter);
		     cellPrice.setCellValue(list.get(i).getPrice());
		     HSSFCell cellStock = row.createCell(5);
		     cellStock.setCellStyle(styleCenter);
		     cellStock.setCellValue(list.get(i).getStock());
		     HSSFCell cellDetail = row.createCell(3);
		     cellDetail.setCellStyle(styleCenter);
		     cellDetail.setCellValue(list.get(i).getDetail());
		   }

		   response.setContentType("applicaiton/x-excel");
		   // 3.以附件的形式返回给浏览器
		   ServletOutputStream outputStream = null;
		   try {
		     response.setHeader("Content-Disposition",
		          "attachment;filename=" + new String("产品列表.xls".getBytes(), "ISO-8859-1"));
		     outputStream = response.getOutputStream();
		     workbook.write(outputStream);
		   } catch (IOException e) {
		     // TODO Auto-generated catch block
		     e.printStackTrace();
		   } finally {
		     // 输出excel到文件
		     if (workbook != null) {
		        try {
		          workbook.close();
		        } catch (IOException e) {
		          e.printStackTrace();
		        }
		     }
		     if (outputStream != null) {
		        try {
		          outputStream.close();
		        } catch (IOException e) {
		          e.printStackTrace();
		        }
		     }
		   }
		}
		private HSSFCellStyle createStyle(HSSFWorkbook workbook, int fontSize) {
		   // 创建单元格样式
		   HSSFCellStyle style = workbook.createCellStyle();
		   style.setAlignment(HSSFCellStyle.ALIGN_CENTER);// center horizontal
		                                 // alignment
		   style.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);// 垂直居中
		   // 创建字体
		   HSSFFont font = workbook.createFont();
		   font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);// 字体加粗
		   font.setFontHeightInPoints((short) fontSize);// 设置字体大小
		   // 加载字体到样式
		   style.setFont(font);
		   return style;
		}



}
